//
//  AppDelegate.m
//  WWXELuShang
//
//  Created by lanou on 15/12/17.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "LeftSortViewController.h"
#import "UserGuideViewController.h"
#import "Reachability.h"
#import "DXAlertView.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize status;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"什么鬼啊什么鬼"]) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"什么鬼啊什么鬼"];
        NSLog(@"第一次启动");
        
        //如果第一次启动的话，使用UserGuideViewController(用户引导页面)作为根视图
        UserGuideViewController *userGuideVC = [[UserGuideViewController alloc]init];
        __weak AppDelegate *weakSelf = self;
        weakSelf.window.rootViewController = userGuideVC;
        //开启网络监听状态
        [[NSNotificationCenter defaultCenter] addObserver:weakSelf selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        [reachability startNotifier];
    }else{
        NSLog(@"不是第一次启动");
        //如果不是第一次启动的话，使用RootViewController作为根视图
        
        //主页面设置
        RootViewController *rootVC = [[RootViewController alloc] init];
        rootVC.title = @"在路上";
        self.mainNC = [[UINavigationController alloc] initWithRootViewController:rootVC];
        self.mainNC.navigationBar.barTintColor = kBarColor;
        [self.mainNC.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        //左抽屉设置
        LeftSortViewController *leftSoutVC = [[LeftSortViewController alloc]init];
        self.leftSlideVC = [[LeftSlideViewController alloc]initWithLeftView:leftSoutVC andMainView:self.mainNC];
        self.window.rootViewController = self.leftSlideVC;
        //开启网络监听状态
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        [reachability startNotifier];
    }
 
    // Override point for customization after application launch.
    return YES;
}

- (void)updateInterfaceWithRwachability:(Reachability *)curReach;
{
    status = [curReach currentReachabilityStatus];
    if (status == NotReachable) {
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" contentText:@"网络连接失败,请检查网络" leftButtonTitle:nil rightButtonTitle:@"确定"];
        [alert show];
    }else if(status == ReachableViaWWAN){
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" contentText:@"请注意，您正在使用手机流量上网" leftButtonTitle:nil rightButtonTitle:@"确定"];
        [alert show];
    }else if(status == ReachableViaWiFi){
        //NSLog(@"网络已连接WiFi信号");
    }
}
- (void)reachabilityChanged:(NSNotification *)note {
    Reachability *currReach = [note object];
    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
    //对连接改变做出响应处理动作
    [self updateInterfaceWithRwachability:currReach];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"回到后台" object:nil];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:@"停车时间到了"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    
}

@end
